// zh_cjdianc/free/freeShake.js
var a = getApp(),
  util = require("../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    alert_show: false,
    free_num: '',
    store_id: '',
    free_rate: ''
  },
  start: function() {
    wx.showToast({
      icon: 'none',
      title: '请用力摇动手机',
    })
    var isShow = true;
    var n = this;
    
    var user_id = wx.getStorageSync('users').id;
    wx.onAccelerometerChange(function(e) {
      if (!isShow) {
        return
      }
      console.log(e.x)
      console.log(e.y)
      console.log(e.z)
      if (e.x > 0.5 && e.y > 0.5) {
        var store_id = n.data.store_id;
        a.util.request({
          url: "entry/wxapp/yyyrun",
          cachetime: "0",
          data: {
            user_id: user_id,
            store_id: store_id
          },
          success: function(t) {
            console.log(t.data)
            if (t.data.code == 0) {
              n.setData({
                alert_show: true,
                free_num: t.data.data.num,
                free_rate: t.data.data.get_rate
              });
              isShow = false;
              wx.showToast({
                title: '摇一摇成功',
                icon: 'success',
                duration: 2000
              })
            } else {
              wx.showToast({
                icon: 'none',
                title: t.data.message,
              })
            }
          }
        });

      }
    })

  },
  enough: function() {
    var n = this;
    var user_id = wx.getStorageSync('users').id;
    a.util.request({
      url: "entry/wxapp/sendaward",
      cachetime: "0",
      data: {
        user_id: user_id,
      },
      success: function(t) {
        console.log(t.data)
        if (t.data.code == 0) {
          wx.reLaunch({
            url: '../myAccount/myAccount?status=2&score=' + t.data.data.score,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: t.data.message,
          })
        }
      }
    });
  },
  agin: function() {
    this.setData({
      alert_show: false
    });
    wx.showToast({
      icon: 'none',
      title: '请用力摇动手机',
    })
    var isShow = true;
    var n = this;
    
    var user_id = wx.getStorageSync('users').id;
    wx.onAccelerometerChange(function(e) {
      if (!isShow) {
        return
      }
      console.log(e.x)
      console.log(e.y)
      console.log(e.z)
      if (e.x > 0.5 && e.y > 0.5) {
        var store_id = n.data.store_id;
        a.util.request({
          url: "entry/wxapp/yyyrun",
          cachetime: "0",
          data: {
            user_id: user_id,
            store_id: store_id
          },
          success: function(t) {
            console.log(t.data)
            if (t.data.code == 0) {
              n.setData({
                alert_show: true,
                free_num: t.data.data.num,
                free_rate: t.data.data.get_rate
              });
              isShow = false;
              wx.showToast({
                title: '摇一摇成功',
                icon: 'success',
                duration: 2000
              })
            } else {
              wx.showToast({
                icon: 'none',
                title: t.data.message,
              })
            }
          }
        });

      }
    })
  },
  goPay: function() {
    var store_id = this.data.store_id;
    wx.navigateTo({
      url: 'freePay?store_id='+store_id,
      success: function(res) {
        wx.hideLoading() //隐藏加载框
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    var n = this;
    var user_id = wx.getStorageSync('users').id;
    n.setData({
      store_id: options.store_id
    })
    a.util.request({
      url: "entry/wxapp/getyyynum",
      cachetime: "0",
      data: {
        user_id: user_id
      },
      success: function(t) {
        console.log(t.data)
        n.setData({
          free_num: t.data.data.num
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})